package com.luoying.itew.util;

import android.util.Log;

import com.luoying.itew.bean.CollectorBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by octopus on 17/6/21.
 */

public class ThreeDUtils {

    //Path:"objPath":"http://localhost:8080/file-server/monitor-point/317199f0e9f44a8d92e083a0afff0c60",
    //"objFile":"male02.obj",
    //"objZipFile":"male02.zip",
    private static String fileServerUri = null;
    private static String objPath = null;
    private static String objFile = null;
    private static String objZipFile = null;
    private static String g3dbFile = null;
    private static int result = 0;

    //文件夹内的名字（包括路径）
    /**@deprecated */
    private static String fileName = null;

    private static List hostX = new ArrayList();
    private static List hostY = new ArrayList();
    private static List hostZ = new ArrayList();
    private static List sensorHostId = new ArrayList();
    private static List<String> collectorNames = new ArrayList();
    private static List<String> startCoordinates = new ArrayList();
    private static List<String> lastCoordinates = new ArrayList();
    private static List<String> collectorWarnStatuses = new ArrayList();
    private static List<String> collectorWarnTime = new ArrayList();
    private static List<Double> accumulativeDisplacements = new ArrayList<>();
    private static List<Integer> collectorWarnList = new ArrayList<>();
    private static List<Integer> collectorStateList = new ArrayList<>();
    private static List<Integer> collectorTypeList = new ArrayList<>();

    //缩放比例
    private static float ratio = 1F;

    public static void  setRatio( float value){
        ratio = value;
    }

    public static float getRatio(){
        return ratio ;
    }


    public static void setModel(JSONObject jsonObject) throws JSONException {
        hostX.clear();
        hostY.clear();
        hostZ.clear();
        sensorHostId.clear();
        collectorNames.clear();
        startCoordinates.clear();
        lastCoordinates.clear();
        collectorWarnStatuses.clear();
        collectorWarnTime.clear();
        accumulativeDisplacements.clear();
        collectorWarnList.clear();
        collectorStateList.clear();

        //JSONObject entity = jsonObject.getJSONObject("resultEntity");
        fileServerUri = jsonObject.getString("fileServerUri");
        JSONObject monitorPoint = jsonObject.getJSONObject("monitorPoint");

        objPath = monitorPoint.getString("objPathApp");
        objFile = monitorPoint.getString("objFileApp");
        g3dbFile = monitorPoint.getString("g3dbFileApp");
        objZipFile = monitorPoint.optString("objZipFileApp");
//        JSONArray jsonArray =  new JSONArray();
        JSONArray jsonArray =  monitorPoint.getJSONArray("allCollectors");
//                monitorPoint.getJSONArray("allCollectors");
        for(int i=0; i < jsonArray.length(); i++) {
            JSONObject user = jsonArray.getJSONObject(i);
            hostX.add( user.getDouble("collectorX"));
            hostY.add( user.getDouble("collectorY"));
            hostZ.add( user.getDouble("collectorZ"));
            sensorHostId.add(user.get("collectorId"));
            collectorNames.add(user.getString("collectorName"));
            collectorTypeList.add(user.getInt("collectorType"));
            if (user.has("startX")) {
                startCoordinates.add(user.getDouble("startX") + "," + user.getDouble("startY") + "," + user.getDouble("startZ"));
            }
            else {
                startCoordinates.add("-");
            }
            if (user.has("lastX")) {
                lastCoordinates.add(user.getDouble("lastX") + "," + user.getDouble("lastY") + "," + user.getDouble("lastZ"));
            }
            else {
                lastCoordinates.add("-");
            }

            CollectorBean collectorBean = new CollectorBean();
            collectorBean.setWarnStatus(user.getInt("warnStatus"));
            collectorBean.setCollectorStatus(user.getInt("collectorStatus"));
            collectorWarnStatuses.add(MonitorUtils.WarnLevelToString(MonitorUtils.CollectorLevel(collectorBean)));

            collectorWarnList.add(user.getInt("warnStatus"));
            collectorStateList.add(user.getInt("collectorStatus"));
            collectorWarnTime.add(user.optString("warnTime"));
            accumulativeDisplacements.add(user.getDouble("accumulativeDisplacement"));
        }
    }

    public static List<Integer> getCollectorTypeList() {
        return collectorTypeList;
    }

    //实际是collector个数
    public static int getSensorNum(){
        return sensorHostId.size();
    }

    public static String getCollectorName(int i) {
        return collectorNames.get(i);
    }

    public static String getStartCoordinate(int i) {
        return startCoordinates.get(i);
    }

    public static String getLastCoordinate(int i) {
        return lastCoordinates.get(i);
    }

    public static String getCollectorWarnStatus(int i) {
        return collectorWarnStatuses.get(i);
    }

    public static String getCollectorWarnTime(int i) {
        String str = collectorWarnTime.get(i);
        return str == null ? "" : str;
    }

    public static double getAccumulativeDisplacements(int i) {
        return accumulativeDisplacements.get(i);
    }

    public static int getHostX(int i){
        return Integer.parseInt(DevideString(hostX.get(i).toString()));
    }
    public static int getHostY(int i){
        return Integer.parseInt(DevideString(hostY.get(i).toString()));
    }
    public static int getHostZ(int i){
        LogUtils.e("s",hostZ.get(i).toString());
        return Integer.parseInt(DevideString(hostZ.get(i).toString()));
    }
    public static int getCollectorState(int i){
        if(collectorStateList.get(i)==1){
            return 1;
        } else {
            return collectorWarnList.get(i)==1?2:collectorWarnList.get(i);
        }
    }
    //Collector id
    public static int getSensorId(int i){
        return (int)sensorHostId.get(i);
    }

    public static String getObjPath() {
        return objPath;
    }

    public static String getFileServerUri() {
        return fileServerUri;
    }

    public static String getObjFile() {
        return objFile;
    }

    /**@deprecated */
    public static String getFileName() {
        return fileName;
    }
    /**@deprecated */
    public static void setFileName(String fileName) {
        ThreeDUtils.fileName = fileName;
    }

    public static String getObjZipFile() {
        return objZipFile;
    }

    public static int getResult() {
        return result;
    }

    public static void setResult(int result) {
        ThreeDUtils.result = result;
    }

    private static List latestData = new ArrayList();
    private static List sensorNo = new ArrayList();
    private static List sensorStatusDesc = new ArrayList();
    private static List sensorTypeDesc = new ArrayList();

    public static void setSensorInfo(JSONObject jsonObject) throws JSONException {
        JSONArray jsonArray = (((jsonObject.getJSONObject("resultEntity")).getJSONObject("sensorHostData"))).getJSONArray("sensors");
        latestData.clear();sensorNo.clear();sensorStatusDesc.clear();sensorTypeDesc.clear();
        for(int i=0;i<jsonArray.length();i++) {
            JSONObject user = null;
            user = (JSONObject) jsonArray.get(i);
            latestData.add(user.getDouble("latestData"));
            sensorNo.add(user.getString("sensorNo"));
            sensorStatusDesc.add(user.getString("sensorStatusDesc"));
            sensorTypeDesc.add(user.getString("sensorTypeDesc"));
        }
    }

    public static String getlatestData(int i){
        return latestData.get(i).toString();
    }
    public static String getsensorNo(int i){
        return sensorNo.get(i).toString();
    }
    public static String getsensorStatusDesc(int i){
        return sensorStatusDesc.get(i).toString();
    }
    public static String getsensorTypeDesc(int i){
        return sensorTypeDesc.get(i).toString();
    }
    public static int getSensorInfoNum(){
        return sensorNo.size();
    }

    //分割字符串中的参数
    private static String DevideString(String x){
        char[] ch = x.toCharArray();
        //定义一个存放最终字符串的StringBuffer
        StringBuffer warnTime = new StringBuffer();
        int location = 0;
        for (int j = 0; j < ch.length; j++) {
            String a = String.valueOf(ch[j]);
            //如果不是？号就把这个字符加在上面定义的StringBuffer
            if (a.equals(".")){
                location = j;}
        }
        if (location == 0){
            return x;
        }else {
            for (int i = 0; i < location; i++){
                warnTime.append(String.valueOf(ch[i]));
                Log.i("x",warnTime.toString());
            }
        }
        return warnTime.toString();
    }

    public static String getG3dbFile() {
        return g3dbFile;
    }
}
